package com.newhopegroup.webcommons.context;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Set;

/**
 * 线程级上下文，限定作用于controller的执行过程中，通过Interceptor实现
 * Created by liusen on 15-7-8.
 */

@Component
public class ContextUtils {

    private final Logger logger = LoggerFactory.getLogger(ContextUtils.class);

    protected ThreadLocal<HashMap<String, Object>> context = new ThreadLocal<HashMap<String, Object>>();

    public boolean isSet() throws ContextException {
        return context.get() != null ? true : false;
    }

    /**
     * 初始化Context
     *
     * @throws ContextException
     */
    public void initContext() throws ContextException {
        context.set(new HashMap<String, Object>());
    }

    /**
     * 设定Context值
     *
     * @param key
     * @param o
     * @throws ContextException
     */
    public void setContextValue(String key, Object o) throws ContextException {
        Preconditions.checkArgument(StringUtils.isNotBlank(key), "key can not be null or empty.");
        Preconditions.checkArgument(o != null, "Object can not be null or empty.");
        Preconditions.checkArgument(context.get() != null, "上下文未初始化,请检查applicationContext.xml中的拦截器配置是否启用,@UseContext注解是否配置正确");
        context.get().put(key, o);
    }

    /**
     * 获取context值
     *
     * @param key
     * @return
     * @throws ContextException
     */
    public Object getContextValue(String key) throws ContextException {
        Preconditions.checkArgument(StringUtils.isNotBlank(key), "key can not be null or empty.");
        Preconditions.checkArgument(context.get() != null, "上下文未初始化,请检查applicationContext.xml中的拦截器配置是否启用,@UseContext注解是否配置正确");
        Preconditions.checkArgument(context.get().containsKey(key), "上下文中不包含请求的key:[" + key + "],请检查@UseContext注解是否配置正确");
        Preconditions.checkArgument(context.get().get(key) != null, "请求的key:[" + key + "],value为空值");
        return context.get().get(key);
    }

    /**
     * 包括keys
     *
     * @param
     * @return
     * @throws ContextException
     */
    public boolean containsContextKey(String key) throws ContextException {
        Preconditions.checkArgument(StringUtils.isNotBlank(key), "key can not be null or empty.");
        Preconditions.checkArgument(context.get() != null, "上下文未初始化,请检查applicationContext.xml中的拦截器配置是否启用");
        Preconditions.checkArgument(context.get().get(key) != null, "请求的key:[" + key + "],value为空值");
        return context.get().containsKey(key);
    }

    /**
     * 获取keys
     *
     * @param
     * @return
     * @throws ContextException
     */
    public Set<String> getContextKeySet() throws ContextException {
        Preconditions.checkArgument(context.get() != null, "上下文未初始化,请检查applicationContext.xml中的拦截器配置是否启用");
        return context.get().keySet();
    }

    /**
     * 关闭Context
     *
     * @throws ContextException
     */
    public void closeContext() throws ContextException {
        context.set(null);
    }


}